package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/users")
    public Result list(UserDto userDto){
        log.info("查询用户: {}", userDto);
        PageResult<User> lists = userService.list(userDto);
        return Result.success(lists);
    }

    /**
     * 新增用户
     */
    @Log
    @PostMapping("/users")
    public Result add(@RequestBody User user){
        log.info("新增用户: {}",user);
        userService.add(user);
        return Result.success();
    }

    /**
     * 删除用户
     */
    @Log
    @DeleteMapping("/users/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("删除用户: {}", ids);
        userService.delete(ids);
        return Result.success();
    }
    /**
     * 根据Id查询用户
     */
    @GetMapping("/users/{id}")
    public Result getById(@PathVariable Integer id){
        User user =userService.getById(id);
        return Result.success(user);
    }

    /**
     * 修改用户
     */
    @Log
    @PutMapping("/users")
    public Result update(@RequestBody User user){
        log.info("修改用户: {}", user);
        userService.update(user);
        return Result.success();
    }

    /**
     * 获取所有用户
     */
    @GetMapping("/users/list")
    public Result getAllList(){
        List<User> userList = userService.getAllList();
        return Result.success(userList);
    }

    /**
     * 根据角色查询用户
     */
    @GetMapping("/users/role/{roleLabel}")
    public Result getByRole(@PathVariable String roleLabel){
        List<User> userList = userService.getByRoleLabel(roleLabel);
        return Result.success(userList);
    }

    /**
     * 根据部门查询用户
     */
    @GetMapping("/users/dept/{deptId}")
    public Result getByDept(@PathVariable Integer deptId){
        List<User> userList = userService.getByDeptId(deptId);
        return Result.success(userList);
    }


}

